package com.im.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.im.service.docManage.smo.IDocManageQuerySMO;
import com.im.service.dto.*;
import com.im.service.projectInfo.smo.IProjectInfoQuerySMO;
import com.im.service.purchasePlan.smo.IPurchasePlanQuerySMO;
import com.im.service.tenderInfo.smo.ITenderInfoOperSMO;
import com.im.util.ListUtil;
import com.im.viewmodel.projectInvitManage.InvitInfoManageOperReqVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.im.service.tenderInfo.smo.ITenderInfoQuerySMO;
import com.im.util.ReturnsUtil;
import com.im.viewmodel.projectInvitManage.InvitInfoManageReqVo;
import com.im.viewmodel.projectInvitManage.InvitInfoManageRspVo;

import net.sf.json.JSONObject;
@RestController
@RequestMapping("/tenderPreparationService")
public class TenderPreparationService extends BaseController{
	
    private static final Logger logger = LogManager.getLogger(TenderPreparationService.class);
    
    @Autowired
    private ITenderInfoQuerySMO tenderInfoQuerySMO;
	@Autowired
	private ITenderInfoOperSMO tenderInfoOperSMO;
	@Autowired
	private IDocManageQuerySMO docManageQuerySMO;
	@Autowired
	private IProjectInfoQuerySMO projectInfoQuerySMO;
	@Autowired
	private IPurchasePlanQuerySMO purchasePlanQuerySMO;

	@RequestMapping(value = "/queryInviteInfo", method = RequestMethod.POST)
	@ResponseBody
    public Object queryTenderInfo( @RequestBody InvitInfoManageReqVo reqVo,SearchOption searchOption){

		try {
			searchOption.setPageNumber(reqVo.getPageNumber());
			searchOption.setPageSize(reqVo.getPageSize());
			PageQuery<InvitInfoManageRspVo> result = tenderInfoQuerySMO.queryInvitInfo(reqVo,searchOption);

			return ReturnsUtil.returnSuccess(result).toString();
		} catch (BaseException eb){
			String resultCode="-1";
			String resultMsg = eb.getMessage();
			return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
		} catch(Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
    	
    }

    
    @RequestMapping(value = "/queryDocManageByParam", method = RequestMethod.POST)
	@ResponseBody
    public String queryDocManageByMap(@RequestBody String jsonStr){
    	if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}
		Map<String,Object> inMap = new HashMap<String,Object>();
		
		try {

			JSONObject jo = JSONObject.fromObject(jsonStr);

			if(jo.containsKey("docManageId") && !"".equals(jo.getString("docManageId"))){
				String docManageId = jo.getString("docManageId");
				inMap.put("docManageId", docManageId);
			}
			if(jo.containsKey("purchPlanId") && !"".equals(jo.getString("purchPlanId"))){
				String purchPlanId = jo.getString("purchPlanId");
				inMap.put("purchasePlanId", purchPlanId);
			}

			List<DocManage> list = docManageQuerySMO.queryDocManageByMap(inMap);
			
			return ReturnsUtil.returnSuccess(list).toString();
		}  catch (BaseException eb){
			String resultCode="-1";
			String resultMsg = eb.getMessage();
			return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
		} catch(Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
    	
    }

	@RequestMapping(value = "/queryDocManageAll", method = RequestMethod.POST)
	@ResponseBody
	public String queryDocManageAll(@RequestBody String jsonStr){
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}
		Map<String,Object> inMap = new HashMap<String,Object>();

		try {
			List<DocManage> list = docManageQuerySMO.queryDocManageList(inMap);

			return ReturnsUtil.returnSuccess(list).toString();
		}  catch (BaseException eb){
			String resultCode="-1";
			String resultMsg = eb.getMessage();
			return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
		} catch(Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}

	}

    @RequestMapping(value = "/queryTenderList", method = RequestMethod.POST)
    @ResponseBody
    public String queryTenderList(@RequestBody String jsonStr){
        if (logger.isInfoEnabled()) {
            logger.info("请求入参json:{}", jsonStr);
        }
        Map<String,Object> inMap = new HashMap<String,Object>();

        try {
            JSONObject jo = JSONObject.fromObject(jsonStr);
            if(jo.containsKey("tenderInfoId") && !"".equals(jo.getString("tenderInfoId"))){
                String tenderInfoId = jo.getString("tenderInfoId");
                inMap.put("tenderInfoId", tenderInfoId);
            }
            if(jo.containsKey("tenderCode") && !"".equals(jo.getString("tenderCode"))){
                String tenderCode = jo.getString("tenderCode");
                inMap.put("tenderCode", tenderCode);
            }
            if(jo.containsKey("tenderTitle") && !"".equals(jo.getString("tenderTitle"))){
                String tenderTitle = jo.getString("tenderTitle");
                inMap.put("tenderTitle", tenderTitle);
            }
            if(jo.containsKey("impleProjId") && !"".equals(jo.getString("impleProjId"))){
                String impleProjId = jo.getString("impleProjId");
                inMap.put("impleProjId", impleProjId);
            }
            if(jo.containsKey("purchPlanId") && !"".equals(jo.getString("purchPlanId"))){
                String purchPlanId = jo.getString("purchPlanId");
                inMap.put("purchPlanId", purchPlanId);
            }

            List<TenderInfo> list = tenderInfoQuerySMO.queryTenderInfoList(inMap);
            if(!ListUtil.isListEmpty(list)){
            	for(TenderInfo tenderInfo:list){
					DocManage docManage = docManageQuerySMO.queryDocManageById(tenderInfo.getDocManageId());
					if(null!=docManage){
						tenderInfo.setDocName(docManage.getDocName());

					}
				}
			}
            return ReturnsUtil.returnSuccess(list).toString();
        }  catch (BaseException eb){
            String resultCode="-1";
            String resultMsg = eb.getMessage();
            return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
        } catch(Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage());
        }

    }

	/**
	 * 新增，开标结果
	 */
	@RequestMapping(value = "/saveResultInfo", method = RequestMethod.POST)
	@ResponseBody
	public Object saveResultInfo(@RequestBody  InvitInfoManageOperReqVo reqVo){

		try {
			tenderInfoOperSMO.saveResultInfo(reqVo);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException eb){
			String resultCode="-1";
			String resultMsg = eb.getMessage();
			return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
		} catch(Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}

	}

	/**
	 * 修改开标结果
	 */
	@RequestMapping(value = "/updateResultInfo",method = RequestMethod.POST)
	@ResponseBody
	public Object updateResultInfo(@RequestBody InvitInfoManageOperReqVo reqVo){
		try {
			tenderInfoOperSMO.updateResultInfo(reqVo);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException eb){
			String resultCode="-1";
			String resultMsg = eb.getMessage();
			return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
		} catch(Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}


	/**
	 * 删除，开标结果
	 */
	@RequestMapping(value = "/deleteResultInfo", method = RequestMethod.POST)
	@ResponseBody
	public Object deleteResultInfo(@RequestBody  InvitInfoManageOperReqVo reqVo){

		try {
			tenderInfoOperSMO.deleteResultInfo(reqVo);
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (BaseException eb){
			String resultCode="-1";
			String resultMsg = eb.getMessage();
			return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
		} catch(Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}

	}

	/**
	 * 查询成功的招标
	 */
	@RequestMapping(value = "/querySuccTendInfoList", method = RequestMethod.POST)
	@ResponseBody
	public Object queryTendInfoList(@RequestBody  String jsonStr){

		Map<String,Object> inMap = new HashMap<String,Object>();

		try {
			JSONObject jo = JSONObject.fromObject(jsonStr);
			if(jo.containsKey("impleProjId") && !"".equals(jo.getString("impleProjId"))){
				String impleProjId = jo.getString("impleProjId");
				inMap.put("impleProjId", impleProjId);
			}
			if(jo.containsKey("purchPlanId") && !"".equals(jo.getString("purchPlanId"))){
				String purchPlanId = jo.getString("purchPlanId");
				inMap.put("purchPlanId", purchPlanId);
			}
			if(jo.containsKey("planProjClass") && !"".equals(jo.getString("planProjClass"))){
				String planProjClass = jo.getString("planProjClass");
				inMap.put("planProjClass", planProjClass);
			}
			if(jo.containsKey("developOrg") && !"".equals(jo.getString("developOrg"))){
				String developOrg = jo.getString("developOrg");
				inMap.put("developOrg", developOrg);
			}
			inMap.put("statusCd", 20);

			List<TenderInfo> list = tenderInfoQuerySMO.queryTenderInfoList(inMap);
			if(!ListUtil.isListEmpty(list)){
				for(TenderInfo ti : list){
					ImplementProject project = new ImplementProject();
					project.setImpleProjId(ti.getImpleProjId());
					ImplementProject retProj = projectInfoQuerySMO.queryImpleProjDetail(project);
					if(retProj!=null){
						ti.setImpleProjName(retProj.getImpleProjName());
					}
				}
			}
			return ReturnsUtil.returnSuccess(list).toString();
		} catch (BaseException eb){
			String resultCode="-1";
			String resultMsg = eb.getMessage();
			return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
		} catch(Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}

	}


	/**
	 * 查询没有发放中标通知书的招标信息
	 */
	@RequestMapping(value = "/querySuccTendInfoListForNotice", method = RequestMethod.POST)
	@ResponseBody
	public Object querySuccTendInfoListForNotice(@RequestBody  String jsonStr){

		Map<String,Object> inMap = new HashMap<String,Object>();

		try {
			JSONObject jo = JSONObject.fromObject(jsonStr);
			if(jo.containsKey("impleProjId") && !"".equals(jo.getString("impleProjId"))){
				String impleProjId = jo.getString("impleProjId");
				inMap.put("impleProjId", impleProjId);
			}
			if(jo.containsKey("purchPlanId") && !"".equals(jo.getString("purchPlanId"))){
				String purchPlanId = jo.getString("purchPlanId");
				inMap.put("purchPlanId", purchPlanId);
			}
			inMap.put("statusCd", 20);

			List<TenderInfo> list = tenderInfoQuerySMO.queryTenderInfoListForNotice(inMap);

			if(jo.containsKey("noticeManageId") && !"".equals(jo.getString("noticeManageId"))){
				String noticeManageId = jo.getString("noticeManageId");
				inMap.put("noticeManageId", noticeManageId);
				List<TenderInfo> lists = tenderInfoQuerySMO.queryTenderInfoForNoticeByNoticeId(inMap);
				if(!ListUtil.isListEmpty(lists)){
					list.addAll(lists);
				}
			}

			for(TenderInfo tenderInfo:list){
				if(null!=tenderInfo.getDocManageId()){
					DocManage docManage = docManageQuerySMO.queryDocManageById(tenderInfo.getDocManageId());
					if(null!=docManage){
						tenderInfo.setDocName(docManage.getDocName());
						tenderInfo.setDocManageId(docManage.getDocManageId());
						tenderInfo.setDocCode(docManage.getDocCode());
					}
				}

				if(null!=tenderInfo.getImpleProjId()){
					ImplementProject req = new ImplementProject();
					req.setImpleProjId(tenderInfo.getImpleProjId());
					ImplementProject implementProject = projectInfoQuerySMO.queryImpleProjDetail(req);
					if(null!=implementProject){
						tenderInfo.setImpleProjName(implementProject.getImpleProjName());
					}
				}

				if(null!=tenderInfo.getPurchPlanId()){
					PurchasePlan req = new PurchasePlan();
					req.setPurchPlanId(tenderInfo.getPurchPlanId());
					PurchasePlan purchasePlan = purchasePlanQuerySMO.queryPurchasePlanDetail(req);
					if(null!=purchasePlan){
						tenderInfo.setPurchPlanName(purchasePlan.getPurchPlanName());
						tenderInfo.setPurchPlanCode(purchasePlan.getPurchPlanCode());
					}
				}
			}
			return ReturnsUtil.returnSuccess(list).toString();
		} catch (BaseException eb){
			String resultCode="-1";
			String resultMsg = eb.getMessage();
			return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
		} catch(Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}

	}

    /**
     * 查询没有开标结果的招标信息
     */
    @RequestMapping(value = "/querySuccTendInfoListForOpenBid", method = RequestMethod.POST)
    @ResponseBody
    public Object querySuccTendInfoListForOpenBid(@RequestBody  String jsonStr){

        Map<String,Object> inMap = new HashMap<String,Object>();

        try {
            JSONObject jo = JSONObject.fromObject(jsonStr);
            if(jo.containsKey("impleProjId") && !"".equals(jo.getString("impleProjId"))){
                String impleProjId = jo.getString("impleProjId");
                inMap.put("impleProjId", impleProjId);
            }
            if(jo.containsKey("purchPlanId") && !"".equals(jo.getString("purchPlanId"))){
                String purchPlanId = jo.getString("purchPlanId");
                inMap.put("purchPlanId", purchPlanId);
            }
//            inMap.put("statusCd", 10);

            List<TenderInfo> list = tenderInfoQuerySMO.querySuccTendInfoListForOpenBid(inMap);

            if(jo.containsKey("noticeManageId") && !"".equals(jo.getString("noticeManageId"))){
                String noticeManageId = jo.getString("noticeManageId");
                inMap.put("noticeManageId", noticeManageId);
                List<TenderInfo> lists = tenderInfoQuerySMO.queryTenderInfoForNoticeByNoticeId(inMap);
                if(!ListUtil.isListEmpty(lists)){
                    list.addAll(lists);
                }
            }

            for(TenderInfo tenderInfo:list){
                if(null!=tenderInfo.getDocManageId()){
                    DocManage docManage = docManageQuerySMO.queryDocManageById(tenderInfo.getDocManageId());
                    if(null!=docManage){
                        tenderInfo.setDocName(docManage.getDocName());
                        tenderInfo.setDocManageId(docManage.getDocManageId());
                        tenderInfo.setDocCode(docManage.getDocCode());
                    }
                }

                if(null!=tenderInfo.getImpleProjId()){
                    ImplementProject req = new ImplementProject();
                    req.setImpleProjId(tenderInfo.getImpleProjId());
                    ImplementProject implementProject = projectInfoQuerySMO.queryImpleProjDetail(req);
                    if(null!=implementProject){
                        tenderInfo.setImpleProjName(implementProject.getImpleProjName());
                    }
                }

                if(null!=tenderInfo.getPurchPlanId()){
                    PurchasePlan req = new PurchasePlan();
                    req.setPurchPlanId(tenderInfo.getPurchPlanId());
                    PurchasePlan purchasePlan = purchasePlanQuerySMO.queryPurchasePlanDetail(req);
                    if(null!=purchasePlan){
                        tenderInfo.setPurchPlanName(purchasePlan.getPurchPlanName());
                        tenderInfo.setPurchPlanCode(purchasePlan.getPurchPlanCode());
                    }
                }
            }
            return ReturnsUtil.returnSuccess(list).toString();
        } catch (BaseException eb){
            String resultCode="-1";
            String resultMsg = eb.getMessage();
            return ReturnsUtil.returnException(resultCode,resultMsg,eb).toString();
        } catch(Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.getMessage());
        }

    }

}
